import preprocess.NewFeatureProcess as NFP
import operator
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder

def refomat(X_features,Y):
    train_feature = X_features['train']
    train_category = Y['train']
    test_feature = X_features['test']
    test_category = Y['test']
    
    all_category = np.unique(np.hstack((train_category,test_category)))
        
    le = LabelEncoder()
    le.fit(all_category)
    
    train_category = le.transform(train_category)
    test_category = le.transform(test_category)
    
    return train_feature,test_feature,train_category,test_category,le


def RF(X_train,Y_train,X_test,Y_test,SetParams=1):
    
    if SetParams == 1:
        N1 = 3
        N2 = 5
        Title =True
        Website = True
        Char = True
        Add = False
        Stopwords = True
        L1 = 2
        L2 = 1
    elif SetParams == 2:
        N1 = 3
        N2 = 5
        Title =True
        Website = True
        Char = False
        Add = False
        Stopwords = True
        L1 = 1
        L2 = 2
    X = {}
    X['train'] = X_train
    X['test'] = X_test
    Y = {}
    Y['train'] = Y_train
    Y['test'] = Y_test
    X_features = NFP.data_to_feature(X,N1,N2,website=Website,title=Title,char=Char,add=Add,stopwords=Stopwords,
                                    least_feature_appear_websites=L1,least_feature_appear_titles=L2)
    
    [Train_feature,Test_feature,Train_category,Test_category,le] = refomat(X_features,Y)
    
    
    lin_clf = RandomForestClassifier(n_estimators=100,random_state=0)
    lin_clf.fit(Train_feature, Train_category) 
    Train_category_prediction = lin_clf.predict(Train_feature)
    print ('RF : Traning Set Accuracy: %f' % accuracy_score(Train_category,Train_category_prediction))
    Test_category_prediction = lin_clf.predict(Test_feature)
    print ('RF : Test Set Accuracy: %f' % accuracy_score(Test_category,Test_category_prediction))
    Y_test_prediction = le.inverse_transform(Test_category_prediction)
    
    return Y_test_prediction